package com.haofengsoft.scce.ui.fragment;

import java.util.*;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshBase;
import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshListView;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.optional.OptionalAdapter;
import com.haofengsoft.scce.adapter.optional.OptionalModel;

/**
 * 自选
 *
 * @author zhj
 */
public class OptionalFragment extends BaseFragment {

    private PullToRefreshListView optionalPtlv;

    private OptionalAdapter optionalAdapter = null;
    private int pageIndex = 0;

    private ImageView refreshIv;

    public static OptionalFragment newInstance(String name) {
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        OptionalFragment fragment = new OptionalFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.main_optional, container, false);
        rootView.setOnTouchListener(new DontSpillOnTouchListener());

        optionalPtlv = (PullToRefreshListView) rootView.findViewById(R.id.optional_ptrl);
        optionalPtlv.setMode(PullToRefreshBase.Mode.BOTH);
        // ImageView searchIv = (ImageView)
        // rootView.findViewById(R.id.optional_title_search_iv);
        refreshIv = (ImageView) rootView.findViewById(R.id.optional_title_refresh_iv);
        refreshIv.setOnClickListener(new RefreshClickListener());
        initVolleyData();
        ImageView nodataIv = (ImageView) rootView.findViewById(R.id.optional_no_data_iv);
        nodataIv.setOnClickListener(new DataLoadOnClickListener());
        optionalPtlv.setEmptyView(nodataIv);
        optionalPtlv.setOnFocusChangeListener(null);
        optionalPtlv.setOnHierarchyChangeListener(null);
        optionalPtlv.setOnRefreshListener(new LoadPullData());

        operatingAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.network_refresh);
        LinearInterpolator lin = new LinearInterpolator();
        operatingAnim.setInterpolator(lin);
        Timer timer = new Timer();
        MyTimerTask task = new MyTimerTask();
        timer.schedule(task, 3000, 3000);
        return rootView;
    }

    Animation operatingAnim;

    class RefreshClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            refreshIv.startAnimation(operatingAnim);
//			refreshIv.clearAnimation();
        }

    }

    class DataLoadOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            initVolleyData();
        }
    }

    class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            messageHandler.sendEmptyMessage(1);
        }
    }

    private Handler messageHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            initVolleyData();
            super.handleMessage(msg);
        }
    };

    private void initVolleyData() {
        optionalAdapter = null;
        List<OptionalModel> modelList = new ArrayList<OptionalModel>();

        OptionalModel optionalModel = new OptionalModel();
        optionalModel.setId("1");
        optionalModel.setName("驻澳五周年型张");
        optionalModel.setNumber("612010");
        optionalModel.setPrice(new Random().nextInt(1000) + ".04");
        optionalModel.setUpAndDown((new Random().nextInt(1000) % 2 == 0 ? "+" : "-") + new Random().nextInt(9) + ".05%");
        optionalModel.setUpOrDown(new Random().nextInt(1000) % 2 == 0);
        modelList.add(optionalModel);

        optionalModel = new OptionalModel();
        optionalModel.setId("2");
        optionalModel.setName("改革双联小型张");
        optionalModel.setNumber("614001");
        optionalModel.setPrice(new Random().nextInt(1000) + ".12");
        optionalModel.setUpAndDown((new Random().nextInt(1000) % 2 == 0 ? "+" : "-") + new Random().nextInt(9) + ".04%");
        optionalModel.setUpOrDown(new Random().nextInt(1000) % 2 == 0);
        modelList.add(optionalModel);

        optionalModel = new OptionalModel();
        optionalModel.setId("2");
        optionalModel.setName("建党90周年小版");
        optionalModel.setNumber("611010");
        optionalModel.setPrice(new Random().nextInt(1000) + ".98");
        optionalModel.setUpAndDown((new Random().nextInt(1000) % 2 == 0 ? "+" : "-") + new Random().nextInt(9) + ".03%");
        optionalModel.setUpOrDown(new Random().nextInt(1000) % 2 == 0);
        modelList.add(optionalModel);

        optionalModel = new OptionalModel();
        optionalModel.setId("3");
        optionalModel.setName("港澳回归金箔型张");
        optionalModel.setNumber("612009");
        optionalModel.setPrice(new Random().nextInt(1000) + ".33");
        optionalModel.setUpAndDown((new Random().nextInt(1000) % 2 == 0 ? "+" : "-") + new Random().nextInt(9) + ".32%");
        optionalModel.setUpOrDown(new Random().nextInt(1000) % 2 == 0);
        modelList.add(optionalModel);

        optionalModel = new OptionalModel();
        optionalModel.setId("4");
        optionalModel.setName("聊斋三小版");
        optionalModel.setNumber("611007");
        optionalModel.setPrice(new Random().nextInt(1000) + ".41");
        optionalModel.setUpAndDown((new Random().nextInt(1000) % 2 == 0 ? "+" : "-") + new Random().nextInt(9) + ".54%");
        optionalModel.setUpOrDown(new Random().nextInt(1000) % 2 == 0);
        modelList.add(optionalModel);

        optionalModel = new OptionalModel();
        optionalModel.setId("5");
        optionalModel.setName("建国四十周年型张");
        optionalModel.setNumber("612008");
        optionalModel.setPrice(new Random().nextInt(1000) + ".24");
        optionalModel.setUpAndDown((new Random().nextInt(1000) % 2 == 0 ? "+" : "-") + new Random().nextInt(9) + ".66%");
        optionalModel.setUpOrDown(new Random().nextInt(1000) % 2 == 0);
        modelList.add(optionalModel);
        refreshData(modelList);

    }

    private void refreshData(List<OptionalModel> modelList) {
        if (null == optionalAdapter) {
            optionalAdapter = new OptionalAdapter(getActivity(), modelList);
            optionalPtlv.setAdapter(optionalAdapter);
        } else {
            optionalAdapter.addOptionalList(modelList);
            optionalAdapter.notifyDataSetChanged();
        }

    }

    class LoadPullData implements PullToRefreshBase.OnRefreshListener2 {
        // 下拉(手势下拉)
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            pageIndex = 0;
            optionalAdapter = null;
            initVolleyData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            initVolleyData();
        }
    }

    @Override
    protected void lazyLoad() {

    }
}
